package com.mj.mafeed.service;

import org.codehaus.jackson.map.ObjectMapper;

import com.mj.mafeed.bean.BookLogJSON;
import com.mj.mafeed.bean.BookLogListJSON;
import com.mj.mafeed.config.Constants;
import com.mj.mafeed.util.HttpUtils;

/**
 * 我的预约
 * @author zhaominglei
 * @date 2016-3-1
 * 
 */
public class BookLogService extends BaseService {
	/**
	 * 我的预约列表
	 */
	public BookLogListJSON getBookLog(String referer, String username, String sign) {
		StringBuilder url = new StringBuilder(Constants.MEMBER_BOOKLOG_URL);
		url.append("?username=").append(username).append("&sign=").append(sign);
		String result = HttpUtils.doGet(url.toString());
		if (result != null && !result.equals("")) {
			try {
				BookLogListJSON bookLogListJSON = new ObjectMapper().readValue(result, BookLogListJSON.class);
				return (bookLogListJSON != null) ? bookLogListJSON : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 预约
	 */
	public boolean book(String referer, BookLogJSON bookLogJSON) {
		if (referer == null || referer.equals("") 
				|| bookLogJSON == null 
				|| bookLogJSON.getProductId() == null 
				|| bookLogJSON.getProductId() < 1) {
			return false;
		}
		StringBuilder param = new StringBuilder();
		param.append("referer=").append(referer).append("&productId=").append(bookLogJSON.getProductId());
		param.append("&quantity=").append(bookLogJSON.getQuantity()).append("&areaName=").append(bookLogJSON.getAreaName());
		param.append("&mobile=").append(bookLogJSON.getMobile()).append("&name=").append(bookLogJSON.getName());
		String result = HttpUtils.doPost(Constants.MEMBER_BOOKLOG_BOOK_URL, param.toString());
		if (result != null && !result.equals("")) {
			if (result.equals("true")) {
				return true;
			}
		}
		return false;
	}	
}
